package m.jclecai.com.activity;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.LayoutRes;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.adapter.base.ViewHolerAdapter;
import m.jclecai.com.adapter.holder.ViewHolder;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.InviteFriendBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * 这里我们要做的一个操作就是显示邀请好友的列表；
 */
public class ShowInvitedFriendActivity extends BaseActivity  {


    @BindView(R.id.pr_listview)
    PullToRefreshListView mPullListView;


    @BindView(R.id.tv_buy_money)
    TextView mTvBuyMoney;

    @BindView(R.id.fl_empty)
    View mFlEmpty;
    @BindView(R.id.tv_commission_money)
    TextView mTvCommissionMoney;
    private ArrayList<InviteFriendBean.RecordsBean> mRecordsBeen;
    private FriendListAdapter mFriendListAdapter;



    @Override
    protected void resetWindow() {
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_show_invited_friend;
    }

    @Override
    protected void initView() {
        super.initView();

        setTitle("邀请列表 ");
        leftOptionText("");

        //mFriendListAdapter = new FriendListAdapter(null);

        mRecordsBeen = new ArrayList<>();
        mFriendListAdapter = new FriendListAdapter(mRecordsBeen);
        mPullListView.setAdapter(mFriendListAdapter);

        mPullListView.setEmptyView(mFlEmpty);

        rightOptionText("我的二维码");
        tvRightOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ShowInvitedFriendActivity.this,InvitedFriendActivity.class));
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        requestFriendList();
        mPullListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page=1;
                requestFriendList();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                requestFriendList();
            }
        });
    }

    int page=1;

    /**
     * 这里我们要做的一个操作就是请求好友列表；
     */
    private void requestFriendList() {
        HashMap<String, String> map = new HashMap<>();
        map.put("user", PreferenceUtil.getUserName(this));
        map.put("password", PreferenceUtil.getPassword(this));

        map.put("page", String.valueOf(page));

        HttpManager.postAsyn(Constant_Url.INVITE_FRIEND_LIST, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                if (mPullListView != null)
                    mPullListView.onRefreshComplete();
            }

            @Override
            public void onResponse(String response) {

                if (!TextUtils.isEmpty(response)) {
                    InviteFriendBean friendBean = FastOk.get(response, InviteFriendBean.class);
                    if ("0".equals(friendBean.getStatus())) {
                        //mFriendListAdapter.refresh(friendBean.getRecords());
                        if(friendBean.getRecords()!=null && !friendBean.getRecords().isEmpty()){
                            if(page==1){
                                mRecordsBeen.clear();
                                //这里是设置内容；
                                mTvBuyMoney.setText(friendBean.getBuy_money_total());
                                mTvCommissionMoney.setText(friendBean.getCommission_money_total());
                            }
                            mRecordsBeen.addAll(friendBean.getRecords());
                            mFriendListAdapter.notifyDataSetChanged();
                            page++;
                        }
                    }
                }
                if (mPullListView != null)
                    mPullListView.onRefreshComplete();
            }
        }, map);
    }


    public class FriendListAdapter extends ViewHolerAdapter<FriendListViewHolder> {

        public List<InviteFriendBean.RecordsBean> mBean;

        public FriendListAdapter(List<InviteFriendBean.RecordsBean> bean) {
            mBean = bean;
        }



        @Override
        public int getCount() {
            return mBean.size();
        }

        @Override
        public FriendListViewHolder onCreateViewHolder(int position, ViewGroup parent) {
            return new FriendListViewHolder(ShowInvitedFriendActivity.this,R.layout.item_friend_list);
        }

        @Override
        public void onBindViewHolder(int position, FriendListViewHolder friendListViewHolder) {
            friendListViewHolder.init(mBean.get(position));
        }
    }

    public class FriendListViewHolder extends ViewHolder{

        private final TextView mName;
        private final TextView mDate;
        private final TextView mCommission;
        private final TextView mBuyMoney;

        public FriendListViewHolder(Context context, @LayoutRes int resource) {
            super(context, resource);

            mName = (TextView) mView.findViewById(R.id.tv_name);
            mDate = (TextView) mView.findViewById(R.id.tv_date);
            mCommission = (TextView) mView.findViewById(R.id.tv_commission);
            mBuyMoney = (TextView) mView.findViewById(R.id.tv_bet_number);

        }

        public void init(InviteFriendBean.RecordsBean bean){
            mName.setText(bean.getUsername());
            mDate.setText(bean.getAddtime());
            mCommission.setText(bean.getCommission_money());
            mBuyMoney.setText(bean.getBuy_money());
        }
    }


}
